package com.nowadays.mobile.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nowadays.mobile.R;
import com.nowadays.mobile.db.DbProvider;
import com.nowadays.mobile.model.Focus;
import com.nowadays.mobile.model.FocusDayList;
import com.nowadays.mobile.ui.MyFocusListView;
import com.nowadays.mobile.utils.ConstsDefine;
import com.nowadays.mobile.utils.FocusUtils;

import java.util.ArrayList;
import java.util.List;

public class FocusListAdapter extends RecyclerView.Adapter<FocusListAdapter.BaseViewHolder> {
  private Context mContext;
  private List<FocusDayList> data = new ArrayList<>();
  public static final int TYPE_HEADER = 0;
  private int todaySeconds;
  private int totalSeconds;
  private static final String Tag = FocusListAdapter.class.getSimpleName();

  @Override
  public int getItemViewType(int position) {
//    return super.getItemViewType(position);
    if (position == 0) {
      return TYPE_HEADER;
    }
    FocusDayList focusDay = getItem(position);
    if (focusDay.getFocusList() == null) {
      return ConstsDefine.FOCUS_TYPE.TYPE_SUM;
    } else {
      return ConstsDefine.FOCUS_TYPE.TYPE_DETAIL;
    }
  }

  public FocusDayList getItem(int position) {
    return data.get(position - 1);
  }


  public FocusListAdapter(Context mContext, List<FocusDayList> focusList) {
    this(mContext);
    setData(focusList);
  }

  public FocusListAdapter(Context mContext) {
    this.mContext = mContext;
  }

  @Override
  public FocusListAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    FocusListAdapter.BaseViewHolder holder;
    if (viewType == TYPE_HEADER) {
      View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_header, parent, false);
      holder = new HeaderViewHolder(view);
    } else if (viewType == ConstsDefine.FOCUS_TYPE.TYPE_SUM) {
      View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_focus_item_sum, parent, false);
      holder = new SumViewHolder(view);
    } else {
      View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_focus_item_detail, parent, false);
      holder = new DetailViewHolder(view);
    }
    return holder;
  }

  private void initTodaySecondsTv(TextView todaySecondsTv) {
    String s = FocusUtils.getMinutesOfSeconds(todaySeconds) + mContext.getString(R.string.minutes);
    SpannableString spanStr = new SpannableString(s);
    spanStr.setSpan(new RelativeSizeSpan(3f), 0, s.length() - 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // set size
    todaySecondsTv.setText(spanStr);
  }

  private void initTotalSecondsTv(TextView totalSecondsTv) {
    String displayTime = FocusUtils.displayInHours(totalSeconds);
    totalSecondsTv.setText(displayTime);
  }

  @Override
  public void onBindViewHolder(FocusListAdapter.BaseViewHolder holder, int position) {
    switch (getItemViewType(position)) {
      case TYPE_HEADER:
        HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
        initTodaySecondsTv(headerViewHolder.todayMinutesTv);
        initTotalSecondsTv(headerViewHolder.totalHourTv);
        break;
      case ConstsDefine.FOCUS_TYPE.TYPE_SUM:
        FocusDayList focusDaySum = getItem(position);
        SumViewHolder sumViewHolder = (SumViewHolder) holder;
        sumViewHolder.dataTv.setText(focusDaySum.getYearMonth());
        int totalSeconds = Integer.valueOf(focusDaySum.getTotalDuration());
        String displayTime = FocusUtils.displayInHours(totalSeconds);
        sumViewHolder.totalFocusTv.setText(displayTime);
        break;
      case ConstsDefine.FOCUS_TYPE.TYPE_DETAIL:
        FocusDayList focusDayDetail = getItem(position);
        DetailViewHolder detailViewHolder = (DetailViewHolder) holder;
        detailViewHolder.dayTv.setText(focusDayDetail.getDay());
        List<Focus> list = focusDayDetail.getFocusList();
        List<Integer> durationList = new ArrayList<>(list.size());
        for (Focus focus : list) {
          int minutes = FocusUtils.getMinutesOfSeconds(focus.getDuration());
          if(minutes > 0) {
            durationList.add(minutes);
          }
        }
        detailViewHolder.focusListView.setFocusList(durationList);
        break;
    }
  }

  @Override
  public int getItemCount() {
    return data.size() + 1;
  }

  public void setData(List<FocusDayList> list) {

    if (list != null && list.size() > 0) {
      int now = (int) (System.currentTimeMillis() / FocusUtils.MILLIS_IN_SECOND);
      todaySeconds = DbProvider.getInstance(mContext).queryTodaySeconds(now);
      totalSeconds = DbProvider.getInstance(mContext).queryTotalSeconds();

      this.data.clear();
      this.data.addAll(list);
      this.notifyDataSetChanged();
    }
  }

  class BaseViewHolder extends RecyclerView.ViewHolder {
    public BaseViewHolder(View itemView) {
      super(itemView);
    }
  }

  class HeaderViewHolder extends BaseViewHolder {
    public TextView todayMinutesTv;
    public TextView totalHourTv;

    public HeaderViewHolder(View itemView) {
      super(itemView);
      todayMinutesTv = (TextView) itemView.findViewById(R.id.today_minutes);
      totalHourTv = (TextView) itemView.findViewById(R.id.focus_total_time);
    }
  }

  class SumViewHolder extends BaseViewHolder {
    public TextView dataTv;
    public TextView totalFocusTv;

    public SumViewHolder(View itemView) {
      super(itemView);
      this.dataTv = (TextView) itemView.findViewById(R.id.sum_data_tv);
      this.totalFocusTv = (TextView) itemView.findViewById(R.id.sum_focus_tv);
    }
  }

  class DetailViewHolder extends BaseViewHolder {
    public TextView dayTv;
    public MyFocusListView focusListView;

    public DetailViewHolder(View itemView) {
      super(itemView);
      this.dayTv = (TextView) itemView.findViewById(R.id.detail_day_tv);
      this.focusListView = (MyFocusListView) itemView.findViewById(R.id.detail_focus_list_view);
    }
  }
}
